package com.gitee.apecome.core.remote.net.netty.server.handler;

import com.gitee.apecome.core.remote.ApeRpcProviderFactory;
import com.gitee.apecome.core.remote.bean.ApeRpcRequest;
import com.gitee.apecome.core.remote.bean.ApeRpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class NettyRequestHandler extends SimpleChannelInboundHandler<ApeRpcRequest> {

    private ApeRpcProviderFactory factory;

    public NettyRequestHandler(ApeRpcProviderFactory factory) {
        this.factory = factory;
    }
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ApeRpcRequest request) throws Exception {
        System.out.println("接收消息" + request);
        ApeRpcResponse response = factory.invoke(request);
        System.out.println("结果计算" + response);
        channelHandlerContext.writeAndFlush(response).sync();
    }

}
